class NotificationModel {
  final String id;
  final String userId;
  final NotificationType type;
  final String senderId;
  final String? postId; // 关联游记ID（可选）
  final bool isRead;
  final DateTime createdAt;

  NotificationModel({
    required this.id,
    required this.userId,
    required this.type,
    required this.senderId,
    this.postId,
    this.isRead = false,
    required this.createdAt,
  });

  factory NotificationModel.fromJson(Map<String, dynamic> json) {
    return NotificationModel(
      id: json['_id']?.toString() ?? json['id']?.toString() ?? '',
      userId: json['user_id']?.toString() ?? '',
      type: NotificationType.fromString(json['type']?.toString() ?? ''),
      senderId: json['sender_id']?.toString() ?? '',
      postId: json['post_id']?.toString(),
      isRead: json['is_read'] ?? false,
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'].toString())
              : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'user_id': userId,
      'type': type.toString(),
      'sender_id': senderId,
      'post_id': postId,
      'is_read': isRead,
      'created_at': createdAt.toIso8601String(),
    };
  }

  NotificationModel copyWith({
    String? id,
    String? userId,
    NotificationType? type,
    String? senderId,
    String? postId,
    bool? isRead,
    DateTime? createdAt,
  }) {
    return NotificationModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      type: type ?? this.type,
      senderId: senderId ?? this.senderId,
      postId: postId ?? this.postId,
      isRead: isRead ?? this.isRead,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  // 标记为已读
  NotificationModel markAsRead() {
    return copyWith(isRead: true);
  }
}

enum NotificationType {
  like,
  comment,
  follow;

  static NotificationType fromString(String value) {
    switch (value.toLowerCase()) {
      case 'like':
        return NotificationType.like;
      case 'comment':
        return NotificationType.comment;
      case 'follow':
        return NotificationType.follow;
      default:
        return NotificationType.like;
    }
  }

  @override
  String toString() {
    switch (this) {
      case NotificationType.like:
        return 'like';
      case NotificationType.comment:
        return 'comment';
      case NotificationType.follow:
        return 'follow';
    }
  }

  String get displayName {
    switch (this) {
      case NotificationType.like:
        return '点赞';
      case NotificationType.comment:
        return '评论';
      case NotificationType.follow:
        return '关注';
    }
  }

  String get icon {
    switch (this) {
      case NotificationType.like:
        return '❤️';
      case NotificationType.comment:
        return '💬';
      case NotificationType.follow:
        return '👤';
    }
  }
}
